package companypay

import (
    "fmt"
	"crypto/tls"
	"wxsdk/wxpay/core"
	. "wxsdk/wxpay"
)

//查询企业付款
type GettransferinfoReq struct {
	AppId          string   `xml:"appid"`             //商户号的appid
	MchId          string   `xml:"mch_id"`            //商户号
	NonceStr       string   `xml:"nonce_str"`         //随机字符串
	Sign           string   `xml:"sign"`              //签名
	ParnetTradeNo  string   `xml:"partner_trade_no"`  //商户订单号
}

type GettransferinfoResponse struct {
	BaseResponse           `xml:",innerXml,omitempty"`
	PartnerTradeNo  string `xml:"partner_trade_no"`        // 商户订单号
	DetailId        string `xml:"detail_id"`               // 付款单号
	Status          string `xml:"status"`                  // 转账状态
	Reason          string `xml:"reason,omitempty"`        // 失败原因
	Openid          string `xml:"openid"`                  // 收款用户openid
	TransferName    string `xml:"transfer_name,omitempty"` // 收款用户姓名
	PaymentAmount   int    `xml:"payment_amount"`          // 付款金额
	TransferTime    string `xml:"transfer_time"`           // 转账时间
	Desc            string `xml:"desc"`                    // 付款描述
}

func Gettransferinfo(appId,mchId,appSecret,orderId,crt,key string) (GettransferinfoResponse,error){
	var (
	    req = new(GettransferinfoReq)
	    rsp GettransferinfoResponse
	)
	cert, err := tls.X509KeyPair([]byte(crt), []byte(key))
	if err != nil {
		return rsp, err
	}
	tlsConfig := &tls.Config{
		Certificates: []tls.Certificate{cert},
	}
	client := core.GetWxCoPayClient(appSecret,tlsConfig)
	//
	req.AppId = appId
	req.MchId = mchId
	req.ParnetTradeNo = orderId
	serverUrl := fmt.Sprintf("https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo")
	err = client.Excute(serverUrl, req, &rsp, false)
    if err != nil {
		return rsp, err
	}
	return rsp, nil
}

	